package com.csun.dsms.utils.pt;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.model.huang.HgInstructionListData;
import com.csun.dsms.model.huang.HgReminderData;
import com.csun.dsms.model.huang.HgUser;
import com.csun.dsms.utils.MD5;
import com.csun.dsms.utils.aes.AESUtil;

import java.util.List;

public class HuangGangUtil {
//    appId： HGS_JJYXDDPT
//- 接口地址： https://gateway.ihuanggang.cn/ee6316bf-ba0b-42c1-8c6f-640db684c4bc
//AES密钥： c7bo5l4buf0ladsu
//- x-hg-api-key： 1830ee2c-3be2-4a23-b725-c16b12097203
//- jssdk地址： https://kfpt.ihuanggang.cn/jssdk/js/jssdk.js

    private static final String appId = "HGS_JJYXDDPT";

    private static final String AESKEY = "c7bo5l4buf0ladsu";

    //https://gateway.ihuanggang.cn/84a6de26dd0f5ec283618a7d91682535
//    private static final String BASEURL = "https://gateway.ihuanggang.cn/84a6de26dd0f5ec283618a7d91682535";
    private static final String BASEURL = "https://gateway.ihuanggang.cn/ee6316bf-ba0b-42c1-8c6f-640db684c4bc";
    private static final String APIKEY = "1830ee2c-3be2-4a23-b725-c16b12097203";

    public static String login(String oToken){
        String result = HuangGangPtReqUtil.httpRequest(BASEURL,APIKEY,oToken);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject!=null && jsonObject.getString("data")!=null){
            String data = jsonObject.getString("data");
            try {
                String aesRes = AESUtil.decrypt(data,AESKEY);
                return aesRes;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }

        return result;
    }

    public static String sendMsg(HgReminderData hgReminderData){
//        - 测试接口地址：
//        http://hgcity-dev.chutianyun.gov.cn:8016/gateway/2f8c7265c4274eca92ec1b583b34fa0d
//        - 正式接口地址： https://gateway.ihuanggang.cn/2f8c7265c4274eca92ec1b583b34fa0d
//        - x-hg-api-key： 13ded457-66f4-4f1d-abb9-df25cc936ebf
        String postUrl = "https://gateway.ihuanggang.cn/2f8c7265c4274eca92ec1b583b34fa0d";
        String apiKey = "13ded457-66f4-4f1d-abb9-df25cc936ebf";
        JSONObject jsObj = new JSONObject();
        jsObj.put("title",hgReminderData.getReminderTitle());
        jsObj.put("source","黄冈市经济运行调度平台");
        jsObj.put("introduction",hgReminderData.getReminderContent());
        jsObj.put("userId",hgReminderData.getIHgUser());
        jsObj.put("messType",1);
        jsObj.put("pushObj",1);
        jsObj.put("linkType",1);
        jsObj.put("pushType",3);
        jsObj.put("linkUrl","http://36.133.17.156:18090/phone/#/");
        String jsonStrParam = jsObj.toJSONString();
        String result = HuangGangPtReqUtil.httpsRequestJson(postUrl,apiKey,jsonStrParam);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject!=null && jsonObject.getString("data")!=null){
            String data = jsonObject.getString("data");
            try {
                return "";
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }

        return result;
    }

    public static String sendJyMsg(HgReminderData hgReminderData){
//        - 测试接口地址：
//        http://hgcity-dev.chutianyun.gov.cn:8016/gateway/2f8c7265c4274eca92ec1b583b34fa0d
//        - 正式接口地址： https://gateway.ihuanggang.cn/2f8c7265c4274eca92ec1b583b34fa0d
//        - x-hg-api-key： 13ded457-66f4-4f1d-abb9-df25cc936ebf
        String postUrl = "https://gateway.ihuanggang.cn/2f8c7265c4274eca92ec1b583b34fa0d";
        String apiKey = "13ded457-66f4-4f1d-abb9-df25cc936ebf";
        JSONObject jsObj = new JSONObject();
        jsObj.put("title",hgReminderData.getReminderTitle());
        jsObj.put("source","学习教育集中整改整治工作调度");
        jsObj.put("introduction",hgReminderData.getReminderContent());
        jsObj.put("userId",hgReminderData.getIHgUser());
        jsObj.put("messType",1);
        jsObj.put("pushObj",1);
        jsObj.put("linkType",1);
        jsObj.put("pushType",3);
        jsObj.put("linkUrl","http://59.110.44.78:18092/");
        String jsonStrParam = jsObj.toJSONString();
        String result = HuangGangPtReqUtil.httpsRequestJson(postUrl,apiKey,jsonStrParam);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject!=null && jsonObject.getString("data")!=null){
            String data = jsonObject.getString("data");
            try {
                return "";
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }

        return result;
    }


    public static String sendMsg(HgInstructionListData hgInstructionListData,String ihgUserId){
//        - 测试接口地址：
//        http://hgcity-dev.chutianyun.gov.cn:8016/gateway/2f8c7265c4274eca92ec1b583b34fa0d
//        - 正式接口地址： https://gateway.ihuanggang.cn/2f8c7265c4274eca92ec1b583b34fa0d
//        - x-hg-api-key： 13ded457-66f4-4f1d-abb9-df25cc936ebf
        if (ihgUserId == null){
            return "该用户未登录i黄冈平台";
        }
        String postUrl = "https://gateway.ihuanggang.cn/2f8c7265c4274eca92ec1b583b34fa0d";
        String apiKey = "13ded457-66f4-4f1d-abb9-df25cc936ebf";
        JSONObject jsObj = new JSONObject();
        jsObj.put("title","领导批示提醒");
        jsObj.put("source","黄冈市经济运行调度平台");
        jsObj.put("introduction",hgInstructionListData.getApprover()+"领导批示,请点击查看");
        jsObj.put("userId",ihgUserId);
        jsObj.put("messType",1);
        jsObj.put("pushObj",1);
        jsObj.put("linkType",1);
        jsObj.put("pushType",3);
        jsObj.put("linkUrl","http://36.133.17.156:18090/phone/#/");
        String jsonStrParam = jsObj.toJSONString();
        String result = HuangGangPtReqUtil.httpsRequestJson(postUrl,apiKey,jsonStrParam);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject!=null && jsonObject.getString("data")!=null){
            String data = jsonObject.getString("data");
            return data;
        }
        return result;
    }




    public static String syncUser(){
        //hgcityCore/home/floatWindowUser
//        张章，蔡宁，杨文志，方为美，皮李星
//        String[] names = {
//                "caining", "chendan", "chenjun", "chenzhenghong", "dengzhonglin",
//                "fangweimei", "gaoweipeng", "haoaifang", "huananyuan", "huguang",
//                "huhao", "hukai", "lichumin", "lijunjie", "liling",
//                "liaojinjun", "liujie", "liutangjun", "liuting", "liuyuxiao",
//                "liuzhongcheng", "maliang", "mayanzhou", "panguolin", "peiyongbo",
//                "xiazhidong", "zhangzhang", "zhaoxiaohu", "zhengguangwen", "zhouli",
//                "yidong", "fengzhiqi" ,"wangxiaobing","tangzhen"
//        };
//        String[] phones = {
//                "15972836299", "18672158196", "15871818999", "13907259080", "13872034288",
//                "18186355688", "13971718061", "13508655326", "15907255899", "18120325835",
//                "13707252908", "18907255888", "13339966557", "13907160801", "18872726966",
//                "18871335577", "13607172207", "13907256006", "13409808499", "13508657728",
//                "13972132636", "13971705274", "13971703676", "15807251168", "15997340232",
//                "13907250667", "13871969999", "13409690362", "13907251618", "13659841123",
//                "18872714299", "16602722600", "15997347209", "15387192637"
//        };
        String[] names = {
                "caining", "fangweimei", "zhangzhang", "yangwenzhi","luoyan",
                "yidong", "fengzhiqi" ,"wangxiaobing","tangzhen","shaoshanyang",
                "yanglei", "hejingxin", "liuyong","jinleijun","fanxueao","yanzhefeng",
                "18972749999","18062211070","18942142266","13636012466","13260644663",
                "18696070901","18671350177","13477668900","13307250175",
                "13872021268", "13971730706", "15907131466", "13871995955"

        };
        String[] phones = {
                "15972836299", "18186355688", "13871969999","18086667678","18827468989",
                "18872714299", "16602722600", "15997347209", "15387192637","18995759186",
                "15098008201", "15926304883", "18171770913","18064172185","13277202000",
                "18907251696","18972749999","18062211070","18942142266","13636012466",
                "13260644663","18696070901","18671350177","13477668900","13307250175",
                "13872021268", "13971730706", "15907131466", "13871995955"
        };
        StringBuilder json = new StringBuilder("[");
        for (int i = 0; i < names.length; i++) {
            if (i > 0) {
                json.append(",");
            }
            json.append("{")
                    .append("\"userName\":\"").append(names[i]).append("\",")
                    .append("\"phoneNum\":\"").append(phones[i]).append("\"")
                    .append("}");
        }
        json.append("]");
        String postJson = json.toString();
        String key = "y8JiwSDcp09JKvr7";
        String iv = "0102030405060711";
        try {
            postJson = AESUtil.encryptAES(postJson, key, iv);
            System.out.println("data: " + postJson);
            //https://gateway.ihuanggang.cn/84a6de26dd0f5ec283618a7d91682535
            String url = "https://gateway.ihuanggang.cn/84a6de26dd0f5ec283618a7d91682535";
//            String url = "http://hgcity-dev.chutianyun.gov.cn:8016/hgapp-core/hgcityCore/home/floatWindowUser";
            JSONObject reqObj = new JSONObject();
            reqObj.put("data",postJson);
            String result = HuangGangPtReqUtil.httpJson(url,reqObj.toJSONString());
            System.out.println("result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        String result = HuangGangPtReqUtil.httpRequest(BASEURL,APIKEY,oToken);
//        JSONObject jsonObject = JSONObject.parseObject(result);
//        if (jsonObject!=null && jsonObject.getString("data")!=null){
//            String data = jsonObject.getString("data");
//            try {
//                String aesRes = AESUtil.decrypt(data,AESKEY);
//                return aesRes;
//            } catch (Exception e) {
//                e.printStackTrace();
//                throw new RuntimeException(e);
//            }
//        }

        return postJson;
    }

    public static void main(String[] args) {
//        String result = login("BH9ySLFaVp9T/mqow/w+p3mTBbhY5QcrP2mC7CVB5D3ez8nJ3gkYHRYj576PWL1afVqNAxfTueWNlSVmDF/hQDUwk27+vrSfnc99OyC5B805skeApJou5HchSPYsuEN+zOS5kOY4SFsdivJSoRQgGA==");
//        System.out.println(result);
        String result = syncUser();
        System.out.println(result);

//        HgReminderData hgReminderData = new HgReminderData();
//        hgReminderData.setReminderTitle("GDP数据上传提醒");
//        hgReminderData.setReminderContent("本月10号该上传GDP指标数据了");
//        hgReminderData.setIHgUser("1834603394435493889");
//        sendMsg(hgReminderData);

    }


    public static void syncUserList(List<HgUser> hgUserList) {
        StringBuilder json = new StringBuilder("[");
        for (int i = 0; i < hgUserList.size(); i++) {
            if (i > 0) {
                json.append(",");
            }
            json.append("{")
                    .append("\"userName\":\"").append(hgUserList.get(i).getRealName()).append("\",")
                    .append("\"phoneNum\":\"").append(hgUserList.get(i).getPhone()).append("\"")
                    .append("}");
        }
        json.append("]");
        String postJson = json.toString();
        String key = "y8JiwSDcp09JKvr7";
        String iv = "0102030405060711";
        try {
            postJson = AESUtil.encryptAES(postJson, key, iv);
            System.out.println("data: " + postJson);
            String url = "https://gateway.ihuanggang.cn/84a6de26dd0f5ec283618a7d91682535";
            JSONObject reqObj = new JSONObject();
            reqObj.put("data",postJson);
            String result = HuangGangPtReqUtil.httpJson(url,reqObj.toJSONString());
            System.out.println("result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
